package hu.codelab.lg.servicecenter.activity;

import hu.codelab.lg.servicecenter.R;
import hu.codelab.lg.servicecenter.ServiceCenterApplication;
import hu.codelab.lg.servicecenter.adapters.ListAdapter;
import hu.codelab.lg.servicecenter.entities.ServiceCenter;
import hu.codelab.lg.servicecenter.entities.ServiceCenters;
import android.content.Intent;
import android.content.res.Resources;
import android.graphics.Typeface;
import android.os.Bundle;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ListView;
import android.widget.RelativeLayout;
import android.widget.TextView;

public final class ListActivity extends BaseActivity {

    private ListView listView;

    @Override
    protected void onCreate(final Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        final Typeface font = Typeface.createFromAsset(getAssets(), "helvetica_neue.ttf");
        final Resources resources = getResources();

        final RelativeLayout layout = (RelativeLayout) getLayoutInflater().inflate(R.layout.activity_list, null);
        setContentView(layout);

        listView = (ListView) layout.findViewById(R.id.lv_list);

        final TextView headerView = (TextView) layout.findViewById(R.id.tv_header_view);
        final TextView leadView = (TextView) layout.findViewById(R.id.tv_header_lg);
        
        headerView.setTypeface(font);
        leadView.setTypeface(font);
        
        try {
            headerView.setText(resources.getString(getPackageManager().getActivityInfo(getComponentName(), 0).labelRes));
        } catch (Exception e) {
            e.printStackTrace();
        }

        setupOfflineList();
    }

    private void setupOfflineList() {
        final ServiceCenters serviceCenters = ((ServiceCenterApplication) getApplication()).getOfflineServiceCenters();

        if (serviceCenters == null || serviceCenters.getServiceCenters() == null || serviceCenters.getServiceCenters().isEmpty()) {
            return;
        }

        listView.setAdapter(new ListAdapter(ListActivity.this, serviceCenters.getServiceCenters()));
        listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {

            @Override
            public void onItemClick(final AdapterView<?> parent, final View view, final int position, final long id) {
                ((ServiceCenterApplication) getApplication()).setCurrentServiceCenter((ServiceCenter) parent.getItemAtPosition(position));
                startActivity(new Intent(ListActivity.this, DetailedActivity.class));
            }

        });
    }

    @SuppressWarnings("unused")
    public void back(final View view) {
        finish();
    }

    @SuppressWarnings("unused")
    public void mainView(final View view) {
        startActivity(new Intent(getBaseContext(), MainActivity.class));
    }
}
